package com.example.demotool.controller;

import org.springframework.cache.annotation.Cacheable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.ArrayList;
import java.util.List;

/**
 * @author zhangyunzhe
 */
@RestController
@RequestMapping("cache")
public class CacheController {

    @Cacheable(cacheNames = "cache1",key = "#num",condition ="#num<2")
    @PostMapping("String")
    public String cache1(@RequestBody int num){
        String s = String.valueOf(num);
        System.out.println(s);
        return s;
    }

    @Cacheable(cacheNames = "cache2",key = "#num",condition ="#num>2")
    @PostMapping("List")
    public List cache2(@RequestBody int num){
        ArrayList<Integer> list = new ArrayList<Integer>(num);
        for (int i = 0; i <num; i++) {
            list.add(i);
        }
        return list;
    }
}
